from sqlalchemy import Column, Integer, String, Float, DateTime, Text
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime

Base = declarative_base()

class ModelTrainingLog(Base):
    """模型训练日志表"""
    __tablename__ = 'model_training_log'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    model = Column(String(32), nullable=False, comment='模型名称')
    train_start = Column(DateTime, nullable=False, comment='训练开始时间')
    train_end = Column(DateTime, nullable=True, comment='训练结束时间')
    status = Column(String(16), nullable=True, default='running', comment='状态(running/completed/failed)')
    epochs = Column(Integer, nullable=True, comment='训练轮数')
    batch_size = Column(Integer, nullable=True, comment='批次大小')
    learning_rate = Column(Float, nullable=True, comment='学习率')
    train_loss = Column(Float, nullable=True, comment='训练损失')
    val_loss = Column(Float, nullable=True, comment='验证损失')
    train_acc = Column(Float, nullable=True, comment='训练准确率')
    val_acc = Column(Float, nullable=True, comment='验证准确率')
    progress = Column(Float, nullable=True, default=0, comment='训练进度(0-100)')
    config_json = Column(Text, nullable=True, comment='训练配置(JSON)')
    created_at = Column(DateTime, nullable=True, default=datetime.now, comment='创建时间')
    
    def __repr__(self):
        return f"<ModelTrainingLog(model='{self.model}', status='{self.status}')>" 